package util;

import android.content.Context;
import android.os.Environment;


import com.hdd.lancity.pad.App;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
 * 创建日期：18-9-8 下午12:09
 * @author machong
 * @company
 * 文件名称： WriteStartApp.java
 * 类说明： 保存运行记录
 */
public class WriteStartApp {

    public void addTxtToFileBuffered( String content) {
        String filePath = null;
        boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (hasSDCard) {
            filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "startApp.txt";
        } else{
            filePath = Environment.getDownloadCacheDirectory().toString() + File.separator + "startApp.txt";
        }
        FileOutputStream outStream = null;
        File file ;
        //在文本文本中追加内容
        BufferedWriter out = null;
        try {
            file = new File(filePath);
            if (!file.exists()) {
                File dir = new File(file.getParent());
                dir.mkdirs();
                file.createNewFile();
            }
            //FileOutputStream(file, true),第二个参数为true是追加内容，false是覆盖
            outStream = new FileOutputStream(file, true);
            out = new BufferedWriter(new OutputStreamWriter(outStream));
            out.newLine();//换行
            out.write(content);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(out != null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

                try {
                    if (null!=outStream) {
                        outStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}
